<?php

use app\lib\Helper;
use PHPUnit\Framework\TestCase;

class TestHelper extends TestCase 
{
    public function testStr2map()
    {
        $cases = [
            'dev1:开发1,test3:测试3' => ['dev1'=>'开发1', 'test3'=>'测试3']
        ];
        foreach ($cases as $in => $excp) {
            $this->assertEquals($excp, Helper::str2map($in));
        }
    }

    public function testUniID()
    {
        $idMap = [];
        $max = 5000; 
        // 单线程循环的唯一id都会重复
        for ($i=0; $i <$max ; $i++) { 
            $id = Helper::uniID();
            $this->assertArrayNotHasKey($id, $idMap, "i=". $i);
            $idMap[$id] = 0;
        }
    }

    public function testGoBlock()
    {
        $cases = [
            [
                [1,2,3,4,5,6,7,8,9,0],
                5,
                [[1,2,3,4,5], [6,7,8,9,0]]
            ],
            [
                [1,2,3,4,5,6,7,8,9,0],
                4,
                [[1,2,3,4], [5,6,7,8],[9,0]]
            ]
        ];
        foreach ($cases as $one) {
            $this->assertEquals($one[2], Helper::goBlock($one[0], $one[1]));
        }
    }

    public function testStrSumC()
    {
        $u = "0123456789ABCDEFGHTIJKLMOPQRSTUVWXYZabcdefghtijklmopqrstuvwxyz";
        $l = "0123456789abcdefghtijklmopqrstuvwxyz";
        for ($i=0; $i < 10000; $i++) { 
            $s = md5(time());

            $c = Helper::strSumC($s);
            $this->assertStringContainsString($c, $l);
            $this->assertEquals($c, Helper::strSumC($s));
            
            $c = Helper::strSumC($s, true);
            $this->assertStringContainsString($c, $u);
            $this->assertEquals($c, Helper::strSumC($s, true));
        }
    }
}